package com.tamo.ppresta.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.tamo.ppresta.R;
import com.tamo.ppresta.base.BaseActivity;
import com.tamo.ppresta.constant.SPConstant;
import com.tamo.ppresta.util.ButtonUtils;
import com.tamo.ppresta.util.LogUtils;
import com.tamo.ppresta.util.MyUtils;
import com.tamo.ppresta.util.SPUtil;
import com.tamo.ppresta.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author yeyang
 * @name ppresta
 * @class name：com.tamo.ppresta.activity
 * @class describe
 * @time 2022/12/18 13:59
 * @change
 * @chang time
 * @class describe
 */
public class SplashActivity extends BaseActivity {
    private static String TAG = SplashActivity.class.getSimpleName();

    @BindView(R.id.iv_splash_icon)
    ImageView mIvSplashIcon;

    private boolean isAgree;
    private boolean isCheck;
    private PopupWindow mYinsiPopupWindow;
    private PopupWindow mUpGradePopWindow;

    @Override
    protected int setLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected void setView() {
        mIvSplashIcon.setOnClickListener(this::onClick);
    }

    @Override
    protected void setData() {

    }

    @Override
    public void onClick(View v) {
        ButtonUtils.isFastDoubleClick();
        switch (v.getId()) {
            case R.id.iv_splash_icon:
                //// TODO: 2022/12/22 判断用户是否登录
//                toClass(SplashActivity.this, LoginActivity.class);
                LogUtils.info(TAG, "跳转到Main页面");
                toClass(SplashActivity.this, MainActivity.class);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + v.getId());
        }
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        LogUtils.info(TAG, ":onWindowFocusChanged");
        if (hasFocus) {
            boolean aBoolean = SPUtil.getBoolean(getApplicationContext(), SPConstant.SP_NAME, SPConstant.SP_XIEYI_AGREE);
            if (!aBoolean) {
                showPopWindow();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private void showPopWindow() {
        // 1、获取用于 PopupWindow 的弹窗view
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View customView = layoutInflater.inflate(R.layout.pop_yinsixieyi, null);
        View layout = layoutInflater.inflate(R.layout.activity_splash, null);

        // 2、创建 PopupWindow 实例，传入 View 及宽高
        mYinsiPopupWindow = new PopupWindow(customView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        // 3、展示
        mYinsiPopupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
        MyUtils.backgroundAlpha(this, 0.5f);

        customView.findViewById(R.id.tv_acepto).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isCheck) {
                    mYinsiPopupWindow.dismiss();
                    MyUtils.backgroundAlpha(SplashActivity.this, 1f);
                    isAgree = true;
                    SPUtil.saveObject(getApplicationContext(), SPConstant.SP_NAME, SPConstant.SP_XIEYI_AGREE, isAgree);
                    showUpGradePopWindow();
                } else {
                    ToastUtil.showShort(SplashActivity.this, "Por favor marque para aceptar");
                }
            }
        });

        customView.findViewById(R.id.tv_not_agree).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SplashActivity.this.finish();
            }
        });

        ImageView ivYinsixieyi = customView.findViewById(R.id.iv_yinsixieyi);
        ivYinsixieyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isCheck) {
                    isCheck = false;
                    ivYinsixieyi.setImageResource(R.mipmap.xiyitankuang_weixuanzhong);
                } else {
                    isCheck = true;
                    ivYinsixieyi.setImageResource(R.mipmap.xiyitankuang_xuanzhong);
                }
            }
        });
    }

    private void showUpGradePopWindow() {
        // 1、获取用于 PopupWindow 的弹窗view
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View customView = layoutInflater.inflate(R.layout.pop_upgrade, null);
        View layout = layoutInflater.inflate(R.layout.activity_splash, null);

        // 2、创建 PopupWindow 实例，传入 View 及宽高
        mUpGradePopWindow = new PopupWindow(customView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        // 3、展示
        mUpGradePopWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
        MyUtils.backgroundAlpha(this, 0.5f);

        customView.findViewById(R.id.tv_de_acuerdo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //升级版本
            }
        });

        customView.findViewById(R.id.tv_rechazada).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUpGradePopWindow.dismiss();
                MyUtils.backgroundAlpha(SplashActivity.this, 1f);
            }
        });
    }
}